import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/http/http.dart';

import '../ManageAddress/ManageAddress.dart';

class AddressCard extends StatefulWidget {
  AddressCard(
      {Key key,
      this.province,
      this.district,
      this.municipality,
      this.shoppingCartSelection,
      this.imageUrl,
      this.name,
      this.addressIntro,
      this.phone,
      this.id,
      this.getaddressL,
      this.removeAddress})
      : super(key: key);
  bool shoppingCartSelection = false;
  String imageUrl = 'assets/images/common/device/监控.png';
  String name = '未收到请求';
  String phone = '13533333333';
  String province;
  String municipality;
  String district;
  String addressIntro;
  ValueChanged<int> getaddressL;
  ValueChanged<int> removeAddress;

  int id;
  @override
  _AddressCardState createState() => _AddressCardState();
}

class _AddressCardState extends State<AddressCard> {
  var imageUrl = 'assets/images/common/device/监控.png';

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(bottom: 20),
      decoration: new BoxDecoration(
          color: Colors.white,
          borderRadius: new BorderRadius.circular((10.0)),
          boxShadow: [
            BoxShadow(
                color: Colors.black12,
                offset: Offset(0.0, 0.0), //阴影xy轴偏移量
                blurRadius: 5.0, //阴影模糊程度
                spreadRadius: 0.1 //阴影扩散程度
                )
          ]),
      // width: 300,
      // height: 60,
      child: Column(children: [
        // 最外层的上到下（上的内容和下面的默认地址）
        Row(
          // 上层的左到右
          children: [
            Container(
              // 图标
              // padding: EdgeInsets.all(8),
              margin: EdgeInsets.all(8),
              height: 50,
              width: 50,
              // color: Colors.yellow,
              child: Image.asset(this.imageUrl),
            ),
            Container(
              // 内部主要内容
              // height: 100,
              margin: EdgeInsets.only(bottom: 10),
              child: Column(children: [
                Container(
                    width: 215,
                    // margin: EdgeInsets.only(left: 0, top: 8),
                    padding: EdgeInsets.only(left: 0, top: 8),
                    child: Row(children: [
                      Container(
                        child: Text(
                          widget.name,
                          style: TextStyle(fontSize: 16),
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.only(left: 8, top: 12),
                        child: Text(
                          '${widget.phone}',
                          style: TextStyle(fontSize: 12, color: Colors.black38),
                        ),
                      ),
                    ])),
                Container(
                  width: 215,
                  child: Text(widget.province +
                      ' ' +
                      widget.municipality +
                      ' ' +
                      widget.district +
                      ' ' +
                      widget.addressIntro),
                ),
              ]),
            ),
            InkWell(
              onTap: () {
                Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) {
                    return ManageAddress(
                      // name: widget.typeId,
                      id: widget.id,
                      name: widget.name,
                      phone: widget.phone,
                      province: widget.province,
                      municipality: widget.municipality,
                      district: widget.district,
                      addressIntro: widget.addressIntro,
                      removeAddress: this._removeAddressHuidiao,
                    ); //返回的是需要跳转单页面
                  },
                ));
              },
              child: Container(
                // 修改的图标
                // color: Colors.red,
                height: 50,
                padding: EdgeInsets.only(left: 10),
                child: Icon(Icons.edit),
              ),
            )
          ],
        ),
        // Divider下划线
        Divider(
          height: 0,
          color: Colors.black87,
          indent: 10,
          endIndent: 10,
        ),
        Container(
            margin: EdgeInsets.only(top: 0, bottom: 0),
            child: Row(children: [
              Theme(
                data: ThemeData(unselectedWidgetColor: Colors.black26),
                child: Checkbox(
                  shape: CircleBorder(), //这里就是实现圆形的设置

                  value: widget.shoppingCartSelection,
                  onChanged: (bool value) async {
                    // widget.invoicing(widget.id);
                    
                    var response = await Http.get(
                        'http://xiangrikui.vaiwan.com/userAddress/setAbsolutePosition?id=${widget.id}');
                    widget.getaddressL(widget.id);

                    setState(() {
                      widget.shoppingCartSelection = value;
                    });
                  },
                ),
              ),
              Text(
                '默认地址',
                style: TextStyle(fontSize: 12, color: Colors.black45),
              ),
              Spacer(),
              InkWell(
                onTap: () async {
                  // Map<String, String> headers = {
                  //   'Authorization':
                  //       'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyNzY4NDAyNTFAcXEuY29tIiwiaWF0IjoxNjI2ODQwMzIxLCJleHAiOjE2Mjc0NDUxMjF9.Xo7DgMFINXSFGKteNqEf0fUIm7ahaIpKKQH5Q6n6LyC6IRsEbeaIMKaFN2D_CIR6-HuKnPHvxNwkxuEKnnkPjQ'
                  // };

                  // Http.setHeaders(headers);
                  // var response = await Http.get(
                  //     'http://xiangrikui.vaiwan.com/userAddress/delete?addressId=${widget.id}');
                  // setState(() {
                  //   widget.removeAddress(widget.id);
                  // });
                  _removeAddressInList();
                },
                child: Container(
                    width: 40,
                    height: 30,
                    padding: EdgeInsets.only(left: 6, top: 6),
                    margin: EdgeInsets.only(right: 8),
                    // color: Colors.red,
                    child: Text(
                      '删除',
                      style: TextStyle(fontSize: 12, color: Colors.black45),
                    )),
              )
            ]))
      ]),
    );
  }

  void _removeAddressInList() async {
    
    var response = await Http.get(
        'http://xiangrikui.vaiwan.com/userAddress/delete?addressId=${widget.id}');
    setState(() {
      widget.removeAddress(widget.id);
    });
    Navigator.pop(context);
  }

  void _removeAddressHuidiao(int id) {
    setState(() {
      widget.removeAddress(widget.id);
    });
  }
}
